package com.group6.exam.teacher.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.group6.exam.common.entity.PageModel;
import com.group6.exam.teacher.entity.SubjectEntity;
import com.group6.exam.teacher.service.ISubjectService;
import com.group6.exam.teacher.service.SubjectServiceImpl;

public class SubjectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String command = request.getParameter("command");
		if ("subjectAdd".equals(command)) {
			subjectAdd(request, response);
		} else if ("query".equals(command)) {
			query(request, response);
		} else if ("delete".equals(command)) {
			delete(request, response);
		} else if ("initUpdate".equals(command)) {
			initUpdate(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		}

	}

	/**
	 * 修改科目信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		String subjectId = request.getParameter("subjectId");
		String subjectName = request.getParameter("subjectName");
		SubjectEntity subjectEntity = new SubjectEntity();
		subjectEntity.setSubjectId(Integer.valueOf(subjectId));
		subjectEntity.setSubjectName(subjectName);
		ISubjectService subjectService = new SubjectServiceImpl();
		int row = subjectService.updateSubjectById(subjectEntity);
		if (row > 0) {
			response.sendRedirect("SubjectServlet?command=query");
		} else {
			response.sendRedirect("SubjectServlet?command=update");
		}
	}

	/**
	 * 修改前初始化信息列表
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void initUpdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		ISubjectService subjectService = new SubjectServiceImpl();

		String subjectId = request.getParameter("subjectId");

		List<Map<String, Object>> subjectInfo = subjectService
				.getsubjectInfoById(subjectId);
		request.setAttribute("subjectInfo", subjectInfo.get(0));

		request.getRequestDispatcher(
				"view/teacher/subject-admin/subject-update.jsp").forward(
				request, response);
	}

	/**
	 * 删除科目信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		ISubjectService subjectInfo = new SubjectServiceImpl();
		String subjectId = request.getParameter("subjectId");
		int row = subjectInfo.deleteSubjectbyId(subjectId);
		if (row > 0) {
			response.sendRedirect("SubjectServlet?command=query");
		} else {
			response.sendRedirect("SubjectServlet?command=query");
		}

	}

	/**
	 * 展示科目信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void query(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 分页
		PageModel pageModel = new PageModel();
		// 获取当前页
		String currentPage = request.getParameter("currentPage");
		int currentPageNo = !"".equals(currentPage) && currentPage != null ? Integer
				.valueOf(currentPage) : 1;
		pageModel.setCurrentPage(currentPageNo);

		ISubjectService subjectInfo = new SubjectServiceImpl();

		String subjectId = request.getParameter("subjectId");
		pageModel.setSubjectId(subjectId);

		List<Map<String, Object>> subjectList = subjectInfo.getSubjectInfo();
		PageModel paging = subjectInfo.check(pageModel);

		request.setAttribute("subjectList", subjectList);
		request.setAttribute("paging", paging);
		request.getRequestDispatcher(
				"view/teacher/subject-admin/subject-maintain.jsp").forward(
				request, response);
	}

	/**
	 * 添加科目信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void subjectAdd(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String sbtId = request.getParameter("sbtId");
		String sbtName = request.getParameter("sbtName");
		SubjectEntity subject = new SubjectEntity();
		subject.setSubjectId(Integer.valueOf(sbtId));
		subject.setSubjectName(sbtName);
		ISubjectService subjectInfo = new SubjectServiceImpl();
		int row = subjectInfo.addSubject(subject);
		if (row > 0) {
			response.sendRedirect("SubjectServlet?command=query");
		} else {
			response.sendRedirect("view/teacher/subject-admin/subject-add.jsp");
		}
	}
}
